package com.hermit.qmst.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hermit.qmst.UI.activity.OpenUrlActivity;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.ImageDownLoader;
import com.hermit.qmst.tools.Rotate3dAnimation;

import java.util.ArrayList;

/**
 * Created by hermithermit on 15/3/6.
 */
public class AdsViewPagerAdapter extends PagerAdapter {

    private ArrayList<ImageView> mImageViewList;
    private ArrayList<String> mImageUrlList;
    private ArrayList<String> mRefreshImageUrlList;
    private Activity mContext;
    private ImageDownLoader mImageDownLoader;
    private ArrayList<String> mImageToUrlList;
    private int mPosition = 0;

    public AdsViewPagerAdapter(ArrayList<String> imageUrlList, ArrayList<String> imageToUrlList, Activity activity) {
        mImageUrlList = imageUrlList;
        mContext = activity;
        mImageDownLoader = new ImageDownLoader(mContext);
        mImageViewList = new ArrayList<>();
        mRefreshImageUrlList = imageUrlList;
        mImageToUrlList = imageToUrlList;

        for (int i = 0; i < 3; i++) {
            ImageView imageView = new ImageView(mContext);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            imageView.setLayoutParams(params);
            imageView.setOnClickListener(new ImageOnClickListener());

            mImageViewList.add(imageView);
        }

    }

    public void setImageUrlList(ArrayList<String> imageUrlList) {
        mRefreshImageUrlList = imageUrlList;

    }

    @Override
    public int getCount() {

        //设置成最大，使用户看不到边界
        if (mImageUrlList.size() == 0) {
            return 0;
        }

        return Integer.MAX_VALUE;
    }


    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {

        mPosition = position;

    }

    @Override
    public boolean isViewFromObject(View view, Object object) {

        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        mImageUrlList = mRefreshImageUrlList;

        //对ViewPager页号求模取出mImageUrlList列表中要显示的项

        int urlPosition = position % mImageUrlList.size();


        //屏幕左划，会出现负数
        if (urlPosition < 0) {
            urlPosition = mImageUrlList.size() + urlPosition;
        }

        String url = mImageUrlList.get(urlPosition);

        mPosition = urlPosition;

        //对ViewPager页号求模取出View列表中要显示的项
        position %= mImageViewList.size();

        //屏幕左划，会出现负数
        if (position < 0) {
            position = mImageViewList.size() + position;
        }


        final ImageView imageView = mImageViewList.get(position);

        //如果View已经在之前添加到了一个父组件，则必须先remove，否则会抛出IllegalStateException。
        ViewParent vp = imageView.getParent();
        if (vp != null) {
            ViewGroup parent = (ViewGroup) vp;
            parent.removeView(imageView);
        }
        container.addView(imageView);

        Bitmap bitmap = mImageDownLoader.downloadImage(Common.getDialImageDirPath(), url, new ImageDownLoader.onImageLoaderListener() {
            @Override
            public void onImageLoader(Bitmap bitmap, String url) {
                Drawable drawable = new BitmapDrawable(mContext.getResources(), bitmap);
                imageView.setBackgroundDrawable(drawable);
                notifyDataSetChanged();
            }
        });

        if (bitmap != null) {
            imageView.setTag(mImageToUrlList.get(urlPosition));
            imageView.setBackgroundDrawable(new BitmapDrawable(mContext.getResources(), bitmap));
        }
        return mImageViewList.get(position);
    }

    @Override
    public void destroyItem(View container, int position, Object object) {

        if (mImageViewList.size() != 0 && position < mImageViewList.size()) {
            ((ViewGroup) container).removeView(mImageViewList.get(position));
        }

    }
    private Rotate3dAnimation applyRotation(float start, float end, float centerX, float centerY) {
            // 计算中心点


        final Rotate3dAnimation rotation =
                new Rotate3dAnimation(start, end, centerX, centerY, 0.0f, true);
        rotation.setDuration(5000);
        rotation.setFillAfter(true);
        rotation.setInterpolator(new AccelerateInterpolator());
        return rotation;
    }

    private class onAnimationListener implements Animation.AnimationListener {

        private int mPosition;

        public onAnimationListener(int position) {
            mPosition = position;
        }


        /**
         * <p>Notifies the start of the animation.</p>
         *
         * @param animation The started animation.
         */
        @Override
        public void onAnimationStart(Animation animation) {

        }

        /**
         * <p>Notifies the end of the animation. This callback is not invoked
         * for animations with repeat count set to INFINITE.</p>
         *
         * @param animation The animation which reached its end.
         */
        @Override
        public void onAnimationEnd(Animation animation) {
            mImageViewList.get(mPosition).clearAnimation();
        }

        /**
         * <p>Notifies the repetition of the animation.</p>
         *
         * @param animation The animation which was repeated.
         */
        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    }


    private class ImageOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {

            if (v.getTag() != null && v.getTag().toString().length() > 0) {
                //if (mPosition < mImageToUrlList.size() && mImageToUrlList.get(mPosition).length() > 0) {
                Intent intent = new Intent(mContext, OpenUrlActivity.class);
                intent.putExtra("title", "");

                intent.putExtra("url", v.getTag().toString());
                //intent.putExtra("url", mImageToUrlList.get(mPosition));
                mContext.startActivity(intent);
                // }

            }
        }
    }

}
